{% extends 'admin/master.html' %}
{% import 'admin/lib.html' as lib with context %}
{% import 'admin/actions.html' as actionlib with context %}

{% block head %}
    <link href="{{ url_for('admin.static', filename='select2/select2.css') }}" rel="stylesheet">
    <link href="{{ url_for('admin.static', filename='css/datepicker.css') }}" rel="stylesheet">
{% endblock %}

{% block body %}
    <ul class="nav nav-tabs">
        <li class="active">
            <a href="#">{{ _gettext('List') }} ({{ count }})</a>
        </li>
        {% if admin_view.can_create %}
        <li>
            <a href="{{ url_for('.create_view', url=return_url) }}">{{ _gettext('Create') }}</a>
        </li>
        {% endif %}

        {% if filter_groups %}
        <li class="dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                {{ _gettext('Add Filter') }}<b class="caret"></b>
            </a>
            <ul class="dropdown-menu field-filters">
                {% for k in filter_groups %}
                <li>
                    <a href="#" class="filter" onclick="return false;">{{ k[0] }}</a>
                </li>
                {% endfor %}
            </ul>
        </li>
        {% endif %}

        {% if actions %}
        <li class="dropdown">
            {{ actionlib.dropdown(actions) }}
        </li>
        {% endif %}

        {% if search_supported %}
        <li>
            <form method="GET" action="{{ return_url }}" class="search-form">
                {% if sort_column is not none %}
                <input type="hidden" name="sort" value="{{ sort_column }}"></input>
                {% endif %}
                {% if sort_desc %}
                <input type="hidden" name="desc" value="{{ sort_desc }}"></input>
                {% endif %}
                <input type="text" name="search" value="{{ search or '' }}" class="search-query span2" placeholder="{{ _gettext('Search') }}"></input>
                {% if search %}
                    <a href="{{ clear_search_url }}" class="clear">
                        <i class="icon-remove"></i>
                    </a>
                {% endif %}
            </form>
        </li>
        {% endif %}
    </ul>
    {% if filter_groups %}
        <form id="filter_form" method="GET" action="{{ return_url }}">
            <div class="pull-right">
                <button type="submit" class="btn btn-primary" style="display: none">{{ _gettext('Apply') }}</button>
                {% if active_filters %}
                <a href="{{ clear_search_url }}" class="btn">{{ _gettext('Reset Filters') }}</a>
                {% endif %}
            </div>

            <div class="filters">
                {%- for i, flt in enumerate(active_filters) -%}
                <div class="filter-row">
                    {% set filter = admin_view._filters[flt[0]] %}
                    <a href="#" class="btn remove-filter" title="{{ _gettext('Remove Filter') }}">
                        <span class="close-icon">&times;</span>&nbsp;{{ filters[flt[0]] }}
                    </a><select class="filter-op" data-role="select2">
                        {% for op in admin_view._filter_dict[filter.name] %}
                        <option value="{{ op[0] }}"{% if flt[0] == op[0] %} selected="selected"{% endif %}>{{ op[1] }}</option>
                        {% endfor %}
                    </select>
                    {%- set data = filter_data.get(flt[0]) -%}
                    {%- if data -%}
                    <select name="flt{{ i }}_{{ flt[0] }}" class="filter-val" data-role="select2">
                        {%- for d in data %}
                        <option value="{{ d[0] }}"{% if flt[1] == d[0] %} selected{% endif %}>{{ d[1] }}</option>
                        {%- endfor %}
                    </select>
                    {%- else -%}
                        <input name="flt{{ i }}_{{ flt[0] }}" type="text" value="{{ flt[1] or '' }}" class="filter-val"{% if flt[0] in filter_types %} data-role="{{ filter_types[flt[0]] }}"{% endif %}></input>
                    {%- endif -%}
                </div>
                {% endfor %}
            </div>
        </form>
    {% endif %}

    <table class="table table-striped table-bordered model-list">
        <thead>
            <tr>
                {% block list_header scoped %}
                    {% if actions %}
                    <th class="span1">
                        <input type="checkbox" name="rowtoggle" class="action-rowtoggle" />
                    </th>
                    {% endif %}
                    <th class="span1">&nbsp;</th>
                    {% set column = 0 %}
                    {% for c, name in list_columns %}
                    <th>
                        {% if admin_view.is_sortable(c) %}
                        {% if sort_column == column %}
                        <a href="{{ sort_url(column, True) }}">
                            {{ name }}
                            {% if sort_desc %}
                            <i class="icon-chevron-up"></i>
                            {% else %}
                            <i class="icon-chevron-down"></i>
                            {% endif %}
                        </a>
                        {% else %}
                        <a href="{{ sort_url(column) }}">{{ name }}</a>
                        {% endif %}
                        {% else %}
                        {{ name }}
                        {% endif %}
                    </th>
                    {% set column = column + 1 %}
                    {% endfor %}
                {% endblock %}
            </tr>
        </thead>
        {% for row in data %}
        <tr>
            {% block list_row scoped %}
                {% if actions %}
                <td>
                    <input type="checkbox" name="rowid" class="action-checkbox" value="{{ get_pk_value(row) }}" />
                </td>
                {% endif %}
                <td>
                    {% block list_row_actions scoped %}
                        {%- if admin_view.can_edit -%}
                        <a class="icon" href="{{ url_for('.edit_view', id=get_pk_value(row), url=return_url) }}">
                            <i class="icon-pencil"></i>
                        </a>
                        {%- endif -%}
                        {%- if admin_view.can_delete -%}
                        <form class="icon" method="POST" action="{{ url_for('.delete_view', id=get_pk_value(row), url=return_url) }}">
                            <button onclick="return confirm('{{ _gettext('You sure you want to delete this item?') }}');">
                                <i class="icon-remove"></i>
                            </button>
                        </form>
                        {%- endif -%}
                    {% endblock %}
                </td>
                {% for c, name in list_columns %}
                <td>{{ get_value(row, c) }}</td>
                {% endfor %}
            {% endblock %}
        </tr>
        {% endfor %}
    </table>
    {{ lib.pager(page, num_pages, pager_url) }}

    {{ actionlib.form(actions, url_for('.action_view')) }}
{% endblock %}

{% block tail %}
    <script src="{{ url_for('admin.static', filename='js/bootstrap-datepicker.js') }}"></script>
    <script src="{{ url_for('admin.static', filename='js/form.js') }}"></script>
    <script src="{{ url_for('admin.static', filename='js/filters.js') }}"></script>

    {{ actionlib.script(_gettext('Please select at least one model.'),
                      actions,
                      actions_confirmation) }}

    {% if filter_groups is not none and filter_data is not none %}
    <script language="javascript">
        (function() {
            var filter = new AdminFilters('#filter_form', '.field-filters',
                                    {{ admin_view._filter_dict|tojson|safe }},
                                    {{ filter_data|tojson|safe }},
                                    {{ filter_types|tojson|safe }});
        })();
    </script>
    {% endif %}
{% endblock %}
